<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:object="{row}">
      <table>
        <tbody>
        <tr>
          <td>项目主体</td>
          <td :colspan="row.value.additional_select_map ? Object.keys(row.value.additional_select_map).length : 1">
            <div class="flex" style="width: 100%;align-items: center;justify-content: center;">
              <img style="height: 36px;" :src="row.value.head_img" />
              <p>{{row.value.title}}</p>
              <p style="margin-left: 2px;color: var(--fail);">￥<b>{{row.value.price-row.value.additional_price}}+{{row.value.deliver_price}}</b></p>
            </div>
          </td>
          <td rowspan="2">数量x{{row.value.num}}</td>
        </tr>
        <tr v-if="row.value.additional_select_map">
          <td>定制配置</td>
          <td v-for="v in row.value.additional_select_map">
            <div class="flex" style="align-items: center;justify-content: center;">
              <img style="height: 36px;" :src="v.head_img" />
              <p>{{v.title}}</p>
              <p style="margin-left: 2px;color: var(--fail);">￥<b>{{v.price}}</b></p>
              <p>（数量x{{v.num}}）</p>
            </div>
          </td>
        </tr>
        </tbody>
      </table>
    </template>
  </forms>
</template>

<script>
export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model = {name:this.$get('model_name'), alias:this.$get('model_alias','')};
    let forms_config = {
      object: {label:model.alias+'详情',name:'object',type:'object',value:{}},
    }
    return {
      id: id,
      model: model,
      btns: btns,
      config: forms_config,
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      data.model_alias = this.model.alias;
      return s94Mjs.param_encode(data);
    },
    onsubmit(formData){
      let url = '/admin/'+this.model.name+'/order_deliver?ids='+this.id;
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('index?'+this.param(), '$refs.list_url.list');
          iframe.hide('deliver');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/'+this.model.name+'/order_info?get_field_config=deliver&id='+this.id}, (res)=>{
        let extend = JSON.parse(res.data.extend);
        res.field_config.cols.forEach((v)=>{
          app.config[v.name] = {
            label: v.label,
            name: v.name,
            tips: v.tips,
            type: v.type,
            is_info: !v.can_deliver,
            config: v.config,
            value:v.value,
          };
        })
        let additional_price = 0;
        if (extend.additional_select_map){
          s94Mjs.each(extend.additional_select_map, function (v){ additional_price += v.price*v.num; })
        }
        res.data.object = {
          title: res.data.title,
          head_img: res.data.head_img,
          num: res.data.num,
          price: res.data.price,
          deliver_price: res.data.deliver_price,
          additional_select_map: extend.additional_select_map,
          additional_price: additional_price,
        }
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('edit?'+this.param());
        }else {
          this.$router.push('index?'+this.param());
        }
      })
    }

    //其他初始化操作
  }
}
</script>
